﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using yiCommerce.Core;
using yiCommerce.Models.Tags;
using yiCommerce.Service.Statuses;
using yiCommerce.Service.Tags;
using StatusMapper = yiCommerce.Mapper.StatusMapper;

namespace yiCommerce.Controllers
{

    public class Tag
    {
        public string id { get; set; }
        public string label { get; set; }
        public string value { get; set; }
    }

    public class TagsController : Controller
    {


        private int pageSize = 5;

        private ITagService _tagService;
        private IStatusService _statusService;
        private IWorkContext _workContext;


        public TagsController(ITagService tagService, IStatusService statusService, IWorkContext workContext)
        {
            _tagService = tagService;
            _statusService = statusService;
            _workContext = workContext;
        }

        public ActionResult Index()
        {
            return RedirectToAction("List");
        }

        public ActionResult List(int? pageIndex)
        {
            var tags = _tagService.FindTags(pageIndex.HasValue ? pageIndex.Value : 0, pageSize);
            var model = new TagListModel {TagLists = StatusMapper.ToPagedTagList(tags)};
            return View(model);

        }

      

        public ActionResult MyInterestedTags(int? pageIndex)
        {
            var tags = _tagService.FindInterestedTags(_workContext.CurrentCustomer.Id, pageIndex.HasValue ? pageIndex.Value : 0, pageSize);
            var model = new TagListModel {TagLists = StatusMapper.ToPagedTagList(tags)};
            return View(model);
        }

        public JsonResult GetTags(string term)
        {
            var tags = _tagService.FindTags(term);
            if (tags != null)
            {
                var result = tags.Select(c => new Tag() { id = c.Id.ToString(), label = c.TagName, value = c.TagValue }).ToList();
                return Json(result, JsonRequestBehavior.AllowGet);
            }
            else
            {
                return Json(null, JsonRequestBehavior.AllowGet);
            }

        }
    }
}
